package com.dyzs.activity.money.bz;

import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dyzs.activity.CommonActivity;
import com.dyzs.activity.R;
import com.dyzs.client.MoneyClient;
import com.dyzs.util.ToolUtils;

public class BZDetailActivity extends CommonActivity {
	private List<Map<String, Object>> detailList;
	private ScrollView mainView;
	private int recordNum = 3;
	private int type;

	private String planId;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		type = getIntent().getIntExtra("type", 0);
		planId = getIntent().getStringExtra("planId");

		mainView = firstView();
		setContentView(mainView);
		update();
	}

	private ScrollView firstView() {
		ScrollView sv = new ScrollView(this);
		sv.setScrollContainer(false);
		
		LinearLayout lay = new LinearLayout(this);
		lay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
		lay.setOrientation(LinearLayout.VERTICAL);

		TextView tv = new TextView(this);
		tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,	LinearLayout.LayoutParams.WRAP_CONTENT));
		lay.addView(tv);		
		sv.addView(lay);
		
		return sv;
	}

	private void update() {
		showProgressDialog(getString(R.string.search_progress_title),getString(R.string.search_progress_content), true, true);
		new Thread(new Runnable() {
			public void run() {
				try {
					MoneyClient moneyClient = new MoneyClient();
					String x = moneyClient.getBZDetail(planId, String.valueOf(type));
					switch(type){
						case 1:detailList = moneyClient.pullBZHotelDetailXml(x);break;
						case 2:detailList = moneyClient.pullBZEatingDetailXml(x);break;
						case 3:detailList = moneyClient.pullBZCarDetailXml(x);break;
						case 4:detailList = moneyClient.pullBZSignDetailXml(x);break;
					}
					
					BZDetailActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							proDialog.dismiss();
							showDetail();
						}
					});
				} catch (Exception e) {
					onException(e);
				}
			}
		}).start();
	}

	public void onException(final Exception e) {
		BZDetailActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}
				LinearLayout top = (LinearLayout) mainView.getChildAt(0);
				((TextView) top.getChildAt(0)).setText(e.getMessage());
			}
		});
	}

	private void showDetail() {
		try{
			LinearLayout top = (LinearLayout) mainView.getChildAt(0);
			if (detailList.isEmpty()) {
				((TextView) top.getChildAt(0)).setText("暂无数据");
			} else {				
				top.removeAllViews();
				recordNum=detailList.size();				
				if (type == 1) {// 酒店
					for (int k = 0; k < recordNum; k++) {
						Map map=detailList.get(k);						
						View ve = LayoutInflater.from(this).inflate(R.layout.bz_hotel, null);
						((TextView)(ve.findViewById(R.id.bz_hotel_name))).setText("酒店    "+map.get("name"));
						((TextView)(ve.findViewById(R.id.bz_hotel_date))).setText("入住时间    "+map.get("date"));
						((TextView)(ve.findViewById(R.id.bz_hotel_dateNum))).setText("天数    "+map.get("dateNum")+"天");
						((TextView)(ve.findViewById(R.id.bz_hotel_total))).setText("小计    "+map.get("total"));						
						((TextView)(ve.findViewById(R.id.bz_hotel_type))).setText("方式    "+(map.get("type").toString().equals("0")?"现付":"挂账"));
						((TextView)(ve.findViewById(R.id.bz_hotel_dfje))).setText("代付金额    "+map.get("total"));						
						((TextView)(ve.findViewById(R.id.bz_sfje))).setText(map.get("sfje").toString());
						((TextView)(ve.findViewById(R.id.bz_remark))).setText(map.get("remark").toString());
						top.addView(ve);
					}
				} else if (type == 2) {// 餐厅
					for (int k = 0; k < recordNum; k++) {
						Map map=detailList.get(k);
						
						View ve = LayoutInflater.from(this).inflate(R.layout.bz_eating, null);
						((TextView)(ve.findViewById(R.id.bz_eating_name))).setText("餐厅名称    "+map.get("ctmc"));
						((TextView)(ve.findViewById(R.id.bz_eating_date))).setText("用餐时间    "+map.get("ycsj"));
						((TextView)(ve.findViewById(R.id.bz_eating_yclx))).setText("用餐类型    "+map.get("lx"));
						((TextView)(ve.findViewById(R.id.bz_eating_num))).setText("餐标及人数    "+map.get("cb")+"×"+map.get("rs"));						
						((TextView)(ve.findViewById(R.id.bz_eating_free))).setText("免餐    "+map.get("mc"));
						((TextView)(ve.findViewById(R.id.bz_eating_total))).setText("小计    "+map.get("total"));	
						((TextView)(ve.findViewById(R.id.bz_eating_dfje))).setText("代付金额    "+map.get("total"));
						((TextView)(ve.findViewById(R.id.bz_eating_type))).setText("方式    "+(map.get("zflx").toString().equals("0")?"现付":"挂账"));
						((TextView)(ve.findViewById(R.id.bz_remark))).setText(map.get("bz").toString());
						((TextView)(ve.findViewById(R.id.bz_sfje))).setText(map.get("sfje").toString());
						top.addView(ve);
					}
				} else if (type == 3) {// 用车
					for (int k = 0; k < recordNum; k++) {
						Map map=detailList.get(k);	
						View ve = LayoutInflater.from(this).inflate(R.layout.bz_car, null);							
						((TextView)(ve.findViewById(R.id.bz_car_start))).setText("用车起    "+map.get("ycq"));
						((TextView)(ve.findViewById(R.id.bz_car_stop))).setText("用车止    "+map.get("ycz"));
						((TextView)(ve.findViewById(R.id.bz_car_price))).setText("车价    "+map.get("cj"));
						((TextView)(ve.findViewById(R.id.bz_car_free))).setText("补贴    "+map.get("bt"));						
						((TextView)(ve.findViewById(R.id.bz_car_lqf))).setText("路桥费    "+map.get("lqf"));
						((TextView)(ve.findViewById(R.id.bz_car_total))).setText("小计    "+map.get("total"));	
						((TextView)(ve.findViewById(R.id.bz_car_type))).setText("方式    "+(map.get("zflx").toString().equals("0")?"现付":"挂账"));
						((TextView)(ve.findViewById(R.id.bz_car_dfje))).setText("代付金额   "+map.get("total"));
						((TextView)(ve.findViewById(R.id.bz_sfje))).setText(map.get("sfje").toString());
						((TextView)(ve.findViewById(R.id.bz_remark))).setText(map.get("bz").toString());
						top.addView(ve);
					}
				} else if (type == 4) {// 景点
					for (int k = 0; k < recordNum; k++) {
						Map map=detailList.get(k);	
						View ve = LayoutInflater.from(this).inflate(R.layout.bz_sign, null);						
						
						((TextView)(ve.findViewById(R.id.bz_sign_name))).setText("景点名称    "+map.get("name"));
						((TextView)(ve.findViewById(R.id.bz_sign_date))).setText("游览时间    "+map.get("d"));
						((TextView)(ve.findViewById(R.id.bz_sign_num))).setText("人数   "+map.get("rs"));	
						((TextView)(ve.findViewById(R.id.bz_sign_adultPrice))).setText("成人价   "+map.get("crj"));	
						((TextView)(ve.findViewById(R.id.bz_sign_adultNum))).setText("人数   "+map.get("crs"));
						((TextView)(ve.findViewById(R.id.bz_sign_childPrice))).setText("儿童价   "+map.get("etj"));	
						((TextView)(ve.findViewById(R.id.bz_sign_childNum))).setText("人数   "+map.get("ets"));
						((TextView)(ve.findViewById(R.id.bz_sign_dfje))).setText("代付金额   "+map.get("total"));
						((TextView)(ve.findViewById(R.id.bz_sign_type))).setText("方式   "+(map.get("type").toString().equals("0")?"现付":"挂账"));
						((TextView)(ve.findViewById(R.id.bz_sign_total))).setText("小计   "+map.get("total"));
						((TextView)(ve.findViewById(R.id.bz_sign_free))).setText("减免   "+map.get("jm"));
						((TextView)(ve.findViewById(R.id.bz_remark))).setText(map.get("remark").toString());
						((TextView)(ve.findViewById(R.id.bz_sfje))).setText(map.get("sfje").toString());						
						top.addView(ve);
					}
				}			
				LinearLayout operll = new LinearLayout(this);
				operll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

				operll.setOrientation(LinearLayout.HORIZONTAL);
				operll.setGravity(Gravity.CENTER_HORIZONTAL);
				operll.addView(createButton("1", "保存"));
				operll.addView(createButton("2", "取消"));
				top.addView(operll);
			}

		}catch(Exception e){
			onException(e);
		}
		
	}


	private Button.OnClickListener l = new Button.OnClickListener() {
		public void onClick(View v) {
			if (v.getTag().equals("1")) {			
				LinearLayout top = (LinearLayout) mainView.getChildAt(0);
				final StringBuilder sb=new StringBuilder();				
				for (int k = 0; k < recordNum; k++) {
					LinearLayout par = (LinearLayout) top.getChildAt(k);					
					EditText et = (EditText) par.findViewById(R.id.bz_sfje);					
					String str=et.getText().toString().trim();
					
					if(!str.equals("") && !str.matches("\\d{1,8}(\\.\\d{1,2})?")){
						ToolUtils.toastText(BZDetailActivity.this, "第"+(k+1)+"报账数据格式错误");
						return ;
					}					
					EditText et1 = (EditText) par.findViewById(R.id.bz_remark);				
					sb.append("<item><id>").append(detailList.get(k).get("id")).append("</id><money>").append(str).append("</money><remark>").append(et1.getText()).append("</remark></item>");
				}
				
				
				showProgressDialog("操作中","操作中,请稍后......", true, true);
				new Thread(new Runnable() {
					public void run() {
						try {
							MoneyClient moneyClient = new MoneyClient();
							final String x = moneyClient.BZ(sb.toString(),type+"");													
							BZDetailActivity.this.runOnUiThread(new Runnable() {
								public void run() {
									proDialog.dismiss();	
									if(x.equals("W")){
										ToolUtils.toastText(BZDetailActivity.this, "操作失败！");
									}else{
										ToolUtils.toastText(BZDetailActivity.this, "操作成功！");
									}
								}
							});
						} catch (Exception e) {
							onException(e);
						}
					}
				}).start();
				
			} else if (v.getTag().equals("2")) {
				showDetail();
			}
		}
	};
	
	
	
	


	private TextView createButton(String tag, String text) {
		Button et = new Button(this);
		et.setText(text);
		et.setTag(tag);
		et.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f));
		et.setGravity(Gravity.CENTER);
		et.setOnClickListener(l);
		return et;
	}
}
